<?php
class advertis
{
	public $table;
	public $pages;
	public $db;
	public $current_user;
	public $typelist;
	
	function advertis()
	{
		global $db,$current_user;
        $this->db = &$db;
        $this->table = DB_PRE.'advertisement';
        $this->current_user = $current_user;
        $this->typelist = array(
        	'text'	=>	array('name'=>'文本'),
        	'image'	=>	array('name'=>'图片', 'allow_type'=>'jpg|jpeg|gif|png', 'allow_size'=>1024*1024),
        	'flash'	=>	array('name'=>'flash', 'allow_type'=>'swf', 'allow_size'=>5*1024*1024),
        	'link'	=>	array('name'=>'文字链')
        );
	}
	
	/**
	 * 插入新广告记录
	 *
	 * @param varchar $adsname
	 * @param varchar $description
	 * @param varchar $adstype
	 * @param varchar $linkurl
	 * @param varchar $adscontent
	 * @return int or false
	 */
	private function _add_advertis($adsname,$description,$adstype,$linkurl,$adscontent)
	{
		$params = array(
			'adsname'		=>	$adsname,
			'description'	=>	$description,
			'adstype'		=>	$adstype,
			'linkurl'		=>	$linkurl,
			'adscontent'	=>	$adscontent,
			'inputtime'		=>	time(),
			'status'		=>	STATUS_NORMAL
		);
		$res = $this->db->insert($this->table,$params);
		return $res;
	}
	
	/**
	 * 添加广告
	 *
	 * @param varchar $adsname
	 * @param varchar $description
	 * @param varchar $adstype
	 * @param varchar $linkurl
	 * @param varchar $adscontent
	 * @param varchar $files
	 * @return bool
	 */
	function add($adsname,$description,$adstype,$linkurl,$adscontent,$files)
	{
		switch ($adstype)
		{
			case 'image':
			case 'flash':
				include_once('../include/attachment.class.php');
				$att = new attachment();
				$res = $att->upload($files,$this->typelist[$adstype]['allow_type'],$this->typelist[$adstype]['allow_size']);
				if($res === false)
				{
					echo $att->error;
					return false;
				}
				$info = $att->get_one($res);
				$adscontent = $info['filepath'];
				break;
			case 'link':
				break;
			case 'text':
			default:
				$linkurl='';
		}
		return $this->_add_advertis($adsname,$description,$adstype,$linkurl,$adscontent);
	}
	
	/**
	 * 添加广告
	 *
	 * @param varchar $adsname
	 * @param varchar $description
	 * @param varchar $adstype
	 * @param varchar $linkurl
	 * @param varchar $adscontent
	 * @param varchar $files
	 * @return bool
	 */
	function edit($aid,$adsname,$description,$adstype,$linkurl,$adscontent,$files)
	{
		$params = array(
			'adsname'		=>	$adsname,
			'description'	=>	$description,
			'adstype'		=>	$adstype
		);
		switch ($adstype)
		{
			case 'image':
			case 'flash':
				$params['linkurl'] = $linkurl;
				if(isset($files) && is_file($_FILES[$files]['tmp_name']))
				{
					include_once('../include/attachment.class.php');
					$att = new attachment();
					$res = $att->upload($files,$this->typelist[$adstype]['allow_type'],$this->typelist[$adstype]['allow_size']);
					if($res === false)
					{
						return false;
					}
					$info = $att->get_one($res);
					$params['adscontent'] = $info['filepath'];
				}
				break;
			case 'link':
				$params['adscontent'] = $adscontent;
				$params['linkurl'] = $linkurl;
				break;
			case 'text':
			default:
				$params['adscontent'] = $adscontent;
				$params['linkurl']='';
		}
		return $this->edit_advertis($aid,$params);
	}
	
	/**
	 * 修改广告记录
	 *
	 * @param int $aid
	 * @param array $params
	 * @return bool
	 */
	function edit_advertis($aid,$params)
	{
		return $this->db->simple_update($this->table,$params,array('adsid'=>$aid));
	}
	
	/**
	 * 根据id获得一条广告
	 *
	 * @param int $aid
	 * @return array
	 */
	function get_advertis_by_id($aid)
	{
		$info = $this->db->get_one_normal($this->table, $aid, 'adsid');
		return $info;
	}
	
	/**
	 * 获得广告列表
	 *
	 * @param int $page
	 * @param int $pagesize
	 * @param int $passed
	 * @param varchar $kw
	 * @param varchar $column
	 * @return array
	 */
	function get_advertis($page,$pagesize,$passed,$kw,$column)
	{
		$start = ($page - 1) * $pagesize;
		$where_array = array(
			'status'	=>	array('set_int',STATUS_NORMAL,'=')
		);
		if(intval($passed) >=0 && $passed !== false)
		{
			$where_array['passed'] = array('set_int',$passed,'=');
		}
		if(!empty($kw))
		{
			$where_array['adsname'] = array('set_string','%'.$kw.'%',' like ');
		}
		$tmp_arr = array();
		foreach ($where_array as $k=>$v)
		{
			$tmp_arr[] = $k.$v[2].'?';
		}
		$where_string = implode(' and ', $tmp_arr);
        $where_string .= " and status=".STATUS_NORMAL;
		unset($tmp_arr);
		$this->db->prepare('select count(*) as nums from '.$this->table . ' where ' . $where_string);
		foreach ($where_array as $where)
		{
			$func = $where[0];
			$this->db->$func($where[1]);
		}
		$c_res = $this->db->execute(true);
		$this->db->prepare('select '.$column . ' from '.$this->table . ' where ' . $where_string . ' order by inputtime asc limit ?,?');
		foreach ($where_array as $where)
		{
			$func = $where[0];
			$this->db->$func($where[1]);
		}
		$this->db->set_int($start);
		$this->db->set_int($pagesize);
		$res = $this->db->execute();
		$cout = $c_res['nums'];
		$this->pages = pages($cout,$page,$pagesize);
		return $res;
	}
	
}
?>